class Solution {
public:
    int numSquares(int n) {
        vector<int> dp(n+1, 0);

        for(int i = 1; i <= n; i++)
        {
            int min_nums = INT_MAX;
            for(int j = 1; j*j <= i; j++)
            {
                min_nums = min(dp[i-j*j], min_nums);
            }
            dp[i] = min_nums+1;
        }

        return dp[n];
    }
};

class Solution {
public:
    int numSquares(int n) {
        vector<int> dp(n+1, INT_MAX);
        dp[0] = 0;
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j*j <= i; j++)
            {
                dp[i] = min(dp[i-j*j]+1, dp[i]);
            }
        }

        return dp[n];
    }
};